﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TRACv2.Common.Enumerations;
using TRACv2.Domain.DTO;
using TRACv2.UI.Facade;

//Author: Morgan William Davis III
//Project: TRAC Ticket Manager v2
//Date: 19 Mar 2014

namespace TRACv2.UI
{
    public partial class FormReports : Form
    {
        #region [ Fields ]

        private readonly TRACFacade _controller;
        private int? _contact, _creator, _assigned;

        #endregion

        #region [ Constructors ]
        
        /// <summary>
        /// Constructor takes TRACFacade object as argument, instantiates the _controller field and populates the datagridview
        /// </summary>
        /// <param name="controller">TRACFacade object</param>
        public FormReports(TRACFacade controller)
        {
            InitializeComponent();
            _controller = controller;
            PopulateGrid(_controller.GetAllTickets(false));
            cmbPriority.DataSource = Enum.GetValues(typeof(Priority));
            cmbStatus.DataSource = Enum.GetValues(typeof(Status));
        }

        #endregion

        #region [ Methods ]

        public void PopulateGrid(List<TicketDTO> ticketList)
        {
            dgvReport.Columns.Clear();
            var col = new DataGridViewButtonColumn();
            col.UseColumnTextForButtonValue = true;
            col.Name = "View Ticket";
            col.Text = "View Ticket";
            dgvReport.Columns.Add(col);
            dgvReport.DataSource = _controller.GetTicketTable(ticketList);
        }


        #region [ Control Events ]

        /// <summary>
        /// Method closes the report window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion

        private void btnReport_Click(object sender, EventArgs e)
        {
            DateTime? start = null, end = null;
            Priority? priority = null;
            Status? status = null;
            Issue? issue = null;
            var assigned = (chkAssigned.Checked) ? _assigned : null;
            var contact = (chkContact.Checked) ? _contact : null;
            var creator = (chkCreator.Checked) ? _creator : null;
            if (chkIssue.Checked)
            {
                if (Enum.GetValues(typeof(Issue)).ToString().Contains(txtIssue.Text))
                {
                    Issue resolvedIssue;
                    if (Issue.TryParse(txtIssue.Text, out resolvedIssue))
                        issue = resolvedIssue;
                }
            }
            if (chkPriority.Checked)
                priority = (Priority?) cmbPriority.SelectedItem;
            if (chkStatus.Checked)
                status = (Status?)cmbStatus.SelectedItem;

            if (chkDateRange.Checked)
            {
                start = dateTimePicker1.Value;
                end = dateTimePicker2.Value;
            }

            var ticketList = _controller.GetAllTickets(true, contact, creator, assigned, issue, priority, status, start, end);
            PopulateGrid(ticketList);

        }

        #endregion

        private void txtContact_Click(object sender, EventArgs e)
        {
            using (var form = new FormEmployeeList(_controller))
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtContact.Text = form.ReturnEmployee;
                    _contact = form.EmployeeObject.ID;
                }
            }
        }

        private void txtCreator_Click(object sender, EventArgs e)
        {
            using (var form = new FormEmployeeList(_controller))
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtCreator.Text = form.ReturnEmployee;
                    _creator = form.EmployeeObject.ID;
                }
            }
        }

        private void txtAssigned_Click(object sender, EventArgs e)
        {
            using (var form = new FormEmployeeList(_controller))
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtAssigned.Text = form.ReturnEmployee;
                    _assigned = form.EmployeeObject.ID;
                }
            }
        }
    }
}
